﻿using System;
using System.Windows.Forms;
namespace YanZhiwei.DotNet2.Utilities.WinForm
{
    /// <summary>
    /// ListBox 帮助类
    /// </summary>
    public static class ListBoxHelper
    {
        #region 为ListBox提供tooltip
        /// <summary>
        /// 为ListBox提供tooltip
        /// </summary>
        /// <param name="lsbox">ListBox</param>
        /// <param name="toolTip">ToolTip</param>
        /// <param name="e">MouseEventArgs</param>
        [Obsolete]
        public static void ItemToolTip(this ListBox lsbox, ToolTip toolTip, MouseEventArgs e)
        {
            int _index = lsbox.IndexFromPoint(e.Location);
            if (_index != -1 && _index < lsbox.Items.Count)
            {
                if (toolTip.GetToolTip(lsbox) != lsbox.Items[_index].ToString())
                {
                    toolTip.IsBalloon = true;
                    toolTip.SetToolTip(lsbox, lsbox.Items[_index].ToString());
                }
            }
        }
        #endregion
        #region item选项右键菜单(MouseUp事件中)
        /// <summary>
        /// item选项右键菜单(MouseUp事件中)
        /// </summary>
        /// <param name="lsbox">ListBox</param>
        /// <param name="e">MouseUp</param>
        /// <param name="contxtMenu">ContextMenuStrip</param>
        [Obsolete]
        public static void ItemRightContxtMenu(this ListBox lsbox, MouseEventArgs e, ContextMenuStrip contxtMenu)
        {
            if (e.Button == MouseButtons.Right)
            {
                int item = lsbox.IndexFromPoint(e.Location);
                if (item >= 0)
                {
                    lsbox.SelectedIndex = item;
                    contxtMenu.Show(lsbox, e.Location);
                }
            }
        }
        #endregion
        #region ITEM上移动，适合不是datasource绑定情况
        /// <summary>
        /// ITEM上移动，适合不是datasource绑定情况
        /// </summary>
        /// <param name="lsbox">ListBox</param>
        public static void ItemMoveUp(this ListBox lsbox)
        {
            int _itemCnt = lsbox.Items.Count;
            int _selectedIndex = lsbox.SelectedIndex;
            if (_itemCnt > _selectedIndex && _selectedIndex > 0)
            {
                object _selectedItem = lsbox.SelectedItem;
                lsbox.Items.RemoveAt(_selectedIndex);
                lsbox.Items.Insert(_selectedIndex - 1, _selectedItem);
                lsbox.SelectedIndex = _selectedIndex - 1;
            }
        }
        #endregion
        #region ITEM下移动，适合不是datasource绑定情况
        /// <summary>
        /// ITEM下移动，适合不是datasource绑定情况
        /// </summary>
        /// <param name="lsbox"></param>
        public static void ItemMoveDown(this ListBox lsbox)
        {
            int _itemCnt = lsbox.Items.Count;
            int _selectedIndex = lsbox.SelectedIndex;
            if (_itemCnt > _selectedIndex && _selectedIndex < _itemCnt - 1)
            {
                object _selectedItem = lsbox.SelectedItem;
                lsbox.Items.RemoveAt(_selectedIndex);
                lsbox.Items.Insert(_selectedIndex + 1, _selectedItem);
                lsbox.SelectedIndex = _selectedIndex + 1;
            }
        }
        #endregion
        #region  添加Items【线程安全】
        /// <summary>
        /// 添加Items【线程安全】
        /// </summary>
        /// <param name="ls">ListBox</param>
        /// <param name="item">object</param>
        public static void AddItemTF(this ListBox ls, object item)
        {
            ls.UIThread((MethodInvoker)delegate
            {
                ls.Items.Add(item);
            });
        }
        #endregion
        #region 添加Items,并且设置是否选中【线程安全】
        /// <summary>
        /// 添加Items,并且设置是否选中【线程安全】
        /// </summary>
        /// <param name="ls">ListBox</param>
        /// <param name="item">object</param>
        /// <param name="selectedIndex">是否选中</param>
        public static void AddItemTF(this ListBox ls, object item, bool selectedIndex)
        {
            ls.UIThread((MethodInvoker)delegate
            {
                ls.Items.Add(item);
                if (selectedIndex)
                    ls.SelectedIndex = ls.Items.Count - 1;
            });
        }
        #endregion 
    }


}
